package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Waiter;
import com.example.demo.mapper.WaiterMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/waitermanager")
public class WaiterController {

    @Resource
    WaiterMapper waiterMapper;

    @PostMapping
    public Result<?> save(@RequestBody Waiter waiter){
        waiterMapper.insert(waiter);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody Waiter waiter){
        waiterMapper.updateById(waiter);
        return Result.success();
    }

    @DeleteMapping("/{waiterid}")
    public Result<?> update(@PathVariable long waiterid){
        waiterMapper.deleteById(waiterid);
        return Result.success();
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Waiter> wrapper = Wrappers.<Waiter>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Waiter::getWaiterid, search);
        }
        Page<Waiter> waiterPage = waiterMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(waiterPage);
    }
}
